package com.automotive.can1939.can;

import com.automotive.can1939.model.CanFrame;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * CAN测试数据生成器
 * 提供常见的汽车CAN测试数据
 */
public class CanTestDataGenerator {

    private static final Random random = new Random();

    /**
     * 获取所有测试数据
     */
    public static List<CanFrame> getAllTestFrames() {
        List<CanFrame> frames = new ArrayList<>();

        frames.addAll(getEngineTestFrames());
        frames.addAll(getVehicleSpeedFrames());
        frames.addAll(getBrakeSystemFrames());
        frames.addAll(getAirConditioningFrames());
        frames.addAll(getLightingSystemFrames());
        frames.addAll(getDoorWindowFrames());

        return frames;
    }

    /**
     * 发动机相关测试数据
     */
    public static List<CanFrame> getEngineTestFrames() {
        List<CanFrame> frames = new ArrayList<>();

        // 发动机转速 (RPM)
        CanFrame rpmFrame = new CanFrame();
        rpmFrame.setCanId(CanConstants.CAN_ID_ENGINE_RPM);
        byte[] rpmData = {0x00, 0x00, (byte) 0x1F, (byte) 0x40, 0x00, 0x00, 0x00, 0x00}; // 2000 RPM
        rpmFrame.setData(rpmData);
        frames.add(rpmFrame);

        // 发动机冷却液温度
        CanFrame tempFrame = new CanFrame();
        tempFrame.setCanId(CanConstants.CAN_ID_ENGINE_TEMP);
        byte[] tempData = {0x00, 0x00, 0x00, (byte) 0x5A, 0x00, 0x00, 0x00, 0x00}; // 90°C
        tempFrame.setData(tempData);
        frames.add(tempFrame);

        // 油门踏板位置
        CanFrame throttleFrame = new CanFrame();
        throttleFrame.setCanId(CanConstants.CAN_ID_THROTTLE_POS);
        byte[] throttleData = {0x00, 0x00, 0x00, 0x00, (byte) 0x32, 0x00, 0x00, 0x00}; // 50%
        throttleFrame.setData(throttleData);
        frames.add(throttleFrame);

        return frames;
    }

    /**
     * 车速相关测试数据
     */
    public static List<CanFrame> getVehicleSpeedFrames() {
        List<CanFrame> frames = new ArrayList<>();

        // 车速信息
        CanFrame speedFrame = new CanFrame();
        speedFrame.setCanId(CanConstants.CAN_ID_VEHICLE_SPEED);
        byte[] speedData = {0x00, 0x00, (byte) 0x19, (byte) 0x00, 0x00, 0x00, 0x00, 0x00}; // 100 km/h
        speedFrame.setData(speedData);
        frames.add(speedFrame);

        // 轮速传感器数据
        CanFrame wheelSpeedFrame = new CanFrame();
        wheelSpeedFrame.setCanId(CanConstants.CAN_ID_WHEEL_SPEED);
        byte[] wheelData = {(byte) 0x64, (byte) 0x64, (byte) 0x64, (byte) 0x64, 0x00, 0x00, 0x00, 0x00}; // 四轮速度
        wheelSpeedFrame.setData(wheelData);
        frames.add(wheelSpeedFrame);

        return frames;
    }

    /**
     * 制动系统测试数据
     */
    public static List<CanFrame> getBrakeSystemFrames() {
        List<CanFrame> frames = new ArrayList<>();

        // 制动踏板状态
        CanFrame brakeFrame = new CanFrame();
        brakeFrame.setCanId(CanConstants.CAN_ID_BRAKE_PEDAL);
        byte[] brakeData = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; // 制动踏板按下
        brakeFrame.setData(brakeData);
        frames.add(brakeFrame);

        // ABS状态
        CanFrame absFrame = new CanFrame();
        absFrame.setCanId(CanConstants.CAN_ID_ABS_STATUS);
        byte[] absData = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}; // ABS激活
        absFrame.setData(absData);
        frames.add(absFrame);

        return frames;
    }

    /**
     * 空调系统测试数据
     */
    public static List<CanFrame> getAirConditioningFrames() {
        List<CanFrame> frames = new ArrayList<>();

        // 空调状态
        CanFrame acFrame = new CanFrame();
        acFrame.setCanId(CanConstants.CAN_ID_AIRCON_STATUS);
        byte[] acData = {0x01, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 开启，22°C
        acFrame.setData(acData);
        frames.add(acFrame);

        // 风扇速度
        CanFrame fanFrame = new CanFrame();
        fanFrame.setCanId(CanConstants.CAN_ID_FAN_SPEED);
        byte[] fanData = {0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00}; // 3档风速
        fanFrame.setData(fanData);
        frames.add(fanFrame);

        return frames;
    }

    /**
     * 照明系统测试数据
     */
    public static List<CanFrame> getLightingSystemFrames() {
        List<CanFrame> frames = new ArrayList<>();

        // 大灯状态
        CanFrame headlightFrame = new CanFrame();
        headlightFrame.setCanId(CanConstants.CAN_ID_HEADLIGHT_STATUS);
        byte[] headlightData = {0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 近光灯开启
        headlightFrame.setData(headlightData);
        frames.add(headlightFrame);

        // 转向灯状态
        CanFrame turnSignalFrame = new CanFrame();
        turnSignalFrame.setCanId(CanConstants.CAN_ID_TURN_SIGNAL);
        byte[] turnData = {0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}; // 左转向灯
        turnSignalFrame.setData(turnData);
        frames.add(turnSignalFrame);

        return frames;
    }

    /**
     * 车门车窗系统测试数据
     */
    public static List<CanFrame> getDoorWindowFrames() {
        List<CanFrame> frames = new ArrayList<>();

        // 车门状态
        CanFrame doorFrame = new CanFrame();
        doorFrame.setCanId(CanConstants.CAN_ID_DOOR_STATUS);
        byte[] doorData = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 所有门关闭
        doorFrame.setData(doorData);
        frames.add(doorFrame);

        // 车窗状态
        CanFrame windowFrame = new CanFrame();
        windowFrame.setCanId(CanConstants.CAN_ID_WINDOW_STATUS);
        byte[] windowData = {(byte) 0x64, (byte) 0x64, (byte) 0x64, (byte) 0x64, 0x00, 0x00, 0x00, 0x00}; // 所有窗户关闭
        windowFrame.setData(windowData);
        frames.add(windowFrame);

        return frames;
    }

    /**
     * 生成随机测试数据
     */
    public static CanFrame generateRandomFrame() {
        int[] testIds = {
                CanConstants.CAN_ID_ENGINE_RPM,
                CanConstants.CAN_ID_ENGINE_TEMP,
                CanConstants.CAN_ID_THROTTLE_POS,
                CanConstants.CAN_ID_VEHICLE_SPEED,
                CanConstants.CAN_ID_WHEEL_SPEED,
                CanConstants.CAN_ID_BRAKE_PEDAL,
                CanConstants.CAN_ID_ABS_STATUS,
                CanConstants.CAN_ID_AIRCON_STATUS,
                CanConstants.CAN_ID_FAN_SPEED,
                CanConstants.CAN_ID_HEADLIGHT_STATUS,
                CanConstants.CAN_ID_TURN_SIGNAL,
                CanConstants.CAN_ID_DOOR_STATUS,
                CanConstants.CAN_ID_WINDOW_STATUS
        };
        int canId = testIds[random.nextInt(testIds.length)];

        byte[] data = new byte[8];
        random.nextBytes(data);

        return new CanFrame(canId, data);
    }

    /**
     * 生成指定CAN ID的测试数据
     */
    public static CanFrame generateFrameWithId(int canId) {
        byte[] data = new byte[8];
        random.nextBytes(data);
        return new CanFrame(canId, data);
    }

    /**
     * 生成指定数据的CAN帧
     */
    public static CanFrame createCustomFrame(int canId, String hexData) {
        CanFrame frame = new CanFrame();
        frame.setCanId(canId);
        frame.setDataFromHexString(hexData);
        return frame;
    }

    /**
     * 获取常用CAN ID列表
     */
    public static int[] getCommonCanIds() {
        return new int[]{
                CanConstants.CAN_ID_ENGINE_RPM,
                CanConstants.CAN_ID_ENGINE_TEMP,
                CanConstants.CAN_ID_THROTTLE_POS,
                CanConstants.CAN_ID_VEHICLE_SPEED,
                CanConstants.CAN_ID_WHEEL_SPEED,
                CanConstants.CAN_ID_BRAKE_PEDAL,
                CanConstants.CAN_ID_ABS_STATUS,
                CanConstants.CAN_ID_AIRCON_STATUS,
                CanConstants.CAN_ID_FAN_SPEED,
                CanConstants.CAN_ID_HEADLIGHT_STATUS,
                CanConstants.CAN_ID_TURN_SIGNAL,
                CanConstants.CAN_ID_DOOR_STATUS,
                CanConstants.CAN_ID_WINDOW_STATUS
        };
    }

    /**
     * 获取CAN ID的描述
     */
    public static String getCanIdDescription(int canId) {
        switch (canId) {
            case CanConstants.CAN_ID_ENGINE_RPM: return "发动机转速";
            case CanConstants.CAN_ID_ENGINE_TEMP: return "发动机温度";
            case CanConstants.CAN_ID_THROTTLE_POS: return "油门位置";
            case CanConstants.CAN_ID_VEHICLE_SPEED: return "车速";
            case CanConstants.CAN_ID_WHEEL_SPEED: return "轮速";
            case CanConstants.CAN_ID_BRAKE_PEDAL: return "制动踏板";
            case CanConstants.CAN_ID_ABS_STATUS: return "ABS状态";
            case CanConstants.CAN_ID_AIRCON_STATUS: return "空调状态";
            case CanConstants.CAN_ID_FAN_SPEED: return "风扇速度";
            case CanConstants.CAN_ID_HEADLIGHT_STATUS: return "大灯状态";
            case CanConstants.CAN_ID_TURN_SIGNAL: return "转向灯";
            case CanConstants.CAN_ID_DOOR_STATUS: return "车门状态";
            case CanConstants.CAN_ID_WINDOW_STATUS: return "车窗状态";
            default: return "未知信号";
        }
    }
}
